﻿using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace MagicControls
{
    public class ScrollableCanvas : CanvasBase
    {
        private readonly CanvasBase innerCanvas = new CanvasBase();
        private readonly ScrollBar verticalScroll = new ScrollBar();
        private readonly ScrollBar horizontalScroll = new ScrollBar();

        public double ScrollableHeight
        {
            get { return innerCanvas.Height; }
            set { innerCanvas.Height = value; }
        }

        public double ScrollableWidth
        {
            get { return innerCanvas.Width; }
            set { innerCanvas.Width = value; }
        }

        public ScrollableCanvas()
        {
            horizontalScroll.ScrollBarType = ScrollBarType.Horizontal.ToString();
            verticalScroll.ScrollBarType = ScrollBarType.Vertical.ToString();
            Loaded += ScrollableCanvas_Loaded;
            SizeChanged += ScrollableCanvas_SizeChanged;
            verticalScroll.ValueChanged += verticalScroll_ValueChanged;
            horizontalScroll.ValueChanged += horizontalScroll_ValueChanged;
        }

        void horizontalScroll_ValueChanged(object sender, EventArgs e)
        {
            innerCanvas.SetValue(LeftProperty, -1 * horizontalScroll.Value);
        }

        void verticalScroll_ValueChanged(object sender, EventArgs e)
        {
            innerCanvas.SetValue(TopProperty, -1 * verticalScroll.Value);
        }

        void ScrollableCanvas_SizeChanged(object o, SizeChangedEventArg eventArg)
        {
            bool enableVerticalScroll = false;
            bool enableHorizontalScroll = false;

            if (innerCanvas.Height > (Height - 15))
            {
                enableVerticalScroll = true;
            }
            if (innerCanvas.Width > (Width - 15))
            {
                enableHorizontalScroll = true;
            }


            if (enableVerticalScroll)
            {
                if (!Children.Contains(verticalScroll)) 
                    Children.Add(verticalScroll);
                verticalScroll.SetValue(TopProperty, 0);
                verticalScroll.SetValue(LeftProperty, Width - 15);
                verticalScroll.Width = 15;
                verticalScroll.Height = enableHorizontalScroll ? Height - 15 : Height;
                verticalScroll.Minimum = 0;
                verticalScroll.Maximum = innerCanvas.Height;
            } else
            {
                if (Children.Contains(verticalScroll))
                    Children.Remove(verticalScroll);
            }

            if (enableHorizontalScroll)
            {
                if (!Children.Contains(horizontalScroll))
                    Children.Add(horizontalScroll);
                horizontalScroll.SetValue(TopProperty, Height - 15);
                horizontalScroll.SetValue(LeftProperty, 0);
                horizontalScroll.Width = enableVerticalScroll ? Width - 15 : Width;
                horizontalScroll.Height = 15;
                horizontalScroll.Minimum = 0;
                horizontalScroll.Maximum = innerCanvas.Width;
            }
            else
            {
                if (Children.Contains(horizontalScroll))
                    Children.Remove(horizontalScroll);
            }
        }

        void ScrollableCanvas_Loaded(object sender, EventArgs e)
        {
            while (Children.Count != 0)
            {
                Visual v = Children[0];
                Children.Remove(v);
                innerCanvas.Children.Add(v);
            }
            Children.Add(innerCanvas);
        }
    }
}